﻿
using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LogDispControl
{
    public enum MessageType
    {
        Info,
        Alarm,
        Warning,
        Important,
    }

    public class Log_Helper
    {
        public delegate void delegateDispProcess(string msg, Color foreColor);
        public event delegateDispProcess eventDispProcess;//消息显示事件

        private string szLogPath;
        private readonly object lock_Txt = new object();
        private readonly object lock_Info = new object();
        private readonly object lock_Warn = new object();

        //时间
        public int tsHour = 2;
        public DateTime dtTxt = new DateTime();
        public DateTime dtLog = new DateTime();
        public DateTime dtWarn = new DateTime();

        //路径
        public string strTxtPath = "";
        public string strInfoPath = "";
        public string strWarnPath = "";
        public string strDataPath = "E:\\Configuration\\Data\\";

        public Log_Helper(string strPath, int iHour)
        {
            tsHour = iHour;
            szLogPath = strPath;
            if (!Directory.Exists(strPath))
            {
                Directory.CreateDirectory(strPath);
            }
        }

        //写入TXT
        public void SaveTxt(string strName, string _contents, bool _append)
        {
            lock (lock_Txt)
            {
                string strPath = strDataPath;
                strPath += strName;
                strPath += "\\";
                strPath += DateTime.Now.ToString("yyyyMMdd");
                strPath += "\\";
                if (!Directory.Exists(strPath))
                {
                    Directory.CreateDirectory(strPath);
                }

                strPath += strName + "_";
                strPath += DateTime.Now.ToString("yyyyMMdd") + ".txt";

                string strThr = ",id[" + Thread.CurrentThread.ManagedThreadId.ToString() + "]";
                string strTxt = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + strThr + "," + _contents;

                if (!_append)
                {
                    File.WriteAllText(strPath, strTxt + "\r\n", Encoding.UTF8);
                }
                else
                {
                    File.AppendAllText(strPath, strTxt + "\r\n", Encoding.UTF8);
                }
            }
        }

        //写入日志
        public void WriteInfo(string _contents)
        {
            lock (lock_Info)
            {
                string strPath = szLogPath;
                strPath += "\\";
                strPath += DateTime.Now.ToString("yyyyMMdd");
                strPath += "\\";
                if (!Directory.Exists(strPath))
                {
                    Directory.CreateDirectory(strPath);
                }

                TimeSpan ts = DateTime.Now.Subtract(dtLog);
                if (ts.Hours >= tsHour)
                {
                    dtLog = DateTime.Now;
                    strPath += "Logs_";
                    strPath += DateTime.Now.ToString("yyyyMMdd_HH") + ".txt";
                    strInfoPath = strPath;
                }
                else
                {
                    if (!File.Exists(strInfoPath))
                    {
                        strPath += "Logs_";
                        strPath += DateTime.Now.ToString("yyyyMMdd_HH") + ".txt";
                        strInfoPath = strPath;
                    }
                }
                File.AppendAllText(strInfoPath, _contents + "\r\n", Encoding.UTF8);
            }
        }

        //写入异常
        public void WriteWarn(string _contents)
        {
            lock (lock_Warn)
            {
                string strPath = szLogPath;
                strPath += "\\";
                strPath += DateTime.Now.ToString("yyyyMMdd");
                strPath += "\\";
                if (!Directory.Exists(strPath))
                {
                    Directory.CreateDirectory(strPath);
                }

                TimeSpan ts = DateTime.Now.Subtract(dtWarn);
                if (ts.Hours >= tsHour)
                {
                    dtWarn = DateTime.Now;
                    strPath += "Warns_";
                    strPath += DateTime.Now.ToString("yyyyMMdd_HH") + ".txt";
                    strWarnPath = strPath;
                }
                else
                {
                    if (!File.Exists(strWarnPath))
                    {
                        strPath += "Warns_";
                        strPath += DateTime.Now.ToString("yyyyMMdd_HH") + ".txt";
                        strWarnPath = strPath;
                    }
                }
                File.AppendAllText(strWarnPath, _contents + "\r\n", Encoding.UTF8);
            }
        }

        //信息日志
        public void DispInfo(string data, MessageType msgType, bool isShown)
        {
            string strDisp = data;
            string strLogText = data;
            string strThread = "id";
            try
            {
                if (eventDispProcess != null)
                {
                    strThread = " id[" + Thread.CurrentThread.ManagedThreadId.ToString() + "]";

                    switch(msgType)
                    {
                        case MessageType.Info:
                            strDisp = "[消息]" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + " " + data + "\n";
                            strLogText = "[消息]" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + strThread + " " + data;
                            if (isShown)
                            {
                                eventDispProcess(strDisp, Color.Black);
                            }
                            break;

                        case MessageType.Alarm:
                            strDisp = "[错误]" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + " " + data + "\n";
                            strLogText = "[错误]" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + strThread + " " + data;
                            if (isShown)
                            {
                                eventDispProcess(strDisp, Color.Red);
                            }
                            break;
                        case MessageType.Warning:
                            strDisp = "[警告]" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + " " + data + "\n";
                            strLogText = "[警告]" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + strThread + " " + data;
                            if (isShown)
                            {
                                eventDispProcess(strDisp, Color.Orange);
                            }
                            break;
                        case MessageType.Important:
                            strDisp = "[重要]" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + " " + data + "\n";
                            strLogText = "[重要]" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + strThread + " " + data;
                            if (isShown)
                            {
                                eventDispProcess(strDisp, Color.Green);
                            }
                            break;
                    }
                    WriteInfo(strLogText);
                }
            }
            catch (Exception ex)
            {
                WriteWarn("函数[DispInfo]异常，原因：" + ex);
            }
        }

        //异常日志
        public void DispWarn(string data, MessageType msgType, bool isShown)
        {
            string strDisp = data;
            string strLogText = data;
            string strThread = "id";

            try
            {
                if (eventDispProcess != null)
                {
                    strThread = " id[" + Thread.CurrentThread.ManagedThreadId.ToString() + "]";
                    if (msgType == MessageType.Info)
                    {
                        strDisp = "[消息]" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + " " + data + "\n";
                        strLogText = "[消息]" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + strThread + " " + data;
                        if (isShown)
                        {
                            eventDispProcess(strDisp, Color.Black);
                        }
                    }
                    else if (msgType == MessageType.Alarm)
                    {
                        strDisp = "[错误]" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + " " + data + "\n";
                        strLogText = "[错误]" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + strThread + " " + data;
                        if (isShown)
                        {
                            eventDispProcess(strDisp, Color.Red);
                        }
                    }
                    WriteWarn(strLogText);
                }
            }
            catch (Exception ex)
            {
                WriteWarn("函数[DispWarn]异常，原因：" + ex.Message);
            }
        }

    }
}
